﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace SurveyHost.Support
{
    public class StmItem
    {
        /// <summary>
        /// A comma, space, semicolon or pipe delimited list of groups the user must belong to at least one of in order to display this menu item
        /// </summary>
        public string Groups { get; set; }
        /// <summary>
        /// The name of the controller whose action will be linked to
        /// </summary>
        public string Controller { get; set; }
        /// <summary>
        /// The text of the link
        /// </summary>
        public string LinkText { get; set; }
        /// <summary>
        /// The action to link to
        /// </summary>
        public string Action { get; set; }
        /// <summary>
        /// The arguments to pass to the Action method.  May be null.
        /// </summary>
        public object Values { get; set; }
        /// <summary>
        /// The attributes to apply to the anchor 
        /// </summary>
        public object Attributes { get; set; }

        /// <summary>
        /// Renders the Html anchor link for this menu item if the current user is a member of one of the given <seealso cref="Groups"/>, or an empty string if not.
        /// </summary>
        /// <param name="helper">The <seealso cref="HtmlHelper"/></param>
        /// <param name="context">The <seealso cref="HttpContextBase">current context</seealso></param>
        /// <returns>The Html anchor link for the given menu item if the user is a member of one of the given <seealso cref="Groups"/>, or the empty string otherwise.</returns>
        /// <exception cref="InvalidArgumentException">Thrown when <paramref name="Groups"/>, <paramref name="Controller"/>, <paramref name="LinkText"/> or <paramref name="Action"/> is null or empty.</exception>
        public string Render(HtmlHelper helper, HttpContextBase context)
        {
            if (helper == null) throw new ArgumentNullException("helper");
            if (context == null) throw new ArgumentNullException("context");
            if (Groups.IsNullOrEmpty() ||
                Controller.IsNullOrEmpty() ||
                LinkText.IsNullOrEmpty() ||
                Action.IsNullOrEmpty())
                throw new InvalidOperationException("You must set Groups, Controller, Action and LinkText.");
            var user = context.User;
            if (user == null) throw new InvalidOperationException("User is null in the given HttpContextBase");
            if (user.Identity == null) throw new InvalidOperationException("The current user's Identity is null in the given HttpContextBase");
            if (!user.Identity.IsAuthenticated) return string.Empty;
            // patented flexisplit all rights reserved
            var groups = Groups.Split(new char[] { ',', '|', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
            var atleastone = (from x in groups where user.IsInRole(x) select x).FirstOrDefault();
            if (atleastone == null)
                return string.Empty;
            return helper.ActionLink(LinkText, Action, Controller, Values, Attributes);
        }
    }
}
